package com.situ.mall.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.dao.CartMapper;
import com.situ.mall.dao.OrderItemMapper;
import com.situ.mall.dao.OrderMapper;
import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.OrderItem;
import com.situ.mall.entity.User;
import com.situ.mall.service.IOrderService;

@Service
public class OrderServiceImpl implements IOrderService{

	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private CartMapper cartMapper;
	
	

	@Override
	public ServerResponse insertOrder(Long orderNo, HttpSession session, Integer shippingId, Integer paymentType,
			BigDecimal payment) {
		User user = (User) session.getAttribute("user");
		Integer userId = user.getId();
		
		Order order = new Order();
		order.setOrderNo(orderNo);
		order.setUserId(userId);
		order.setShippingId(shippingId);
		order.setPaymentType(paymentType);
		order.setPayment(payment);
		
		int count = orderMapper.insert(order);
		
		int cartDelCount = cartMapper.deleteCartByUserIdAndChecked(userId);
		
		if (count == 1) {
			return ServerResponse.createSuccess("订单提交成功");
		}
		
		return ServerResponse.createError("订单提交失败");
	}

	@Override
	public List<Order> selectOrderByUserId(Integer userId) {
		return orderMapper.selectOrderByUserId(userId);
	}
	
	

}
